package com.all.ui.adapter;

import java.util.List;
import com.all.ui.R;
import com.all.widget.tree.Node;
import com.all.widget.tree.OnTreeNodeClickListener;
import com.all.widget.tree.TreeHelper;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class DynaTreeAdapter extends BaseAdapter {

	protected LayoutInflater mInflater;
	/**
	 * 存储所有的Node
	 */
	protected List<Node> mAllNodes;
	/**
	 * 存储所有可见的Node
	 */
	protected List<Node> mNodes;
	/**
	 * 保存点击的按钮
	 */
	protected Node mClickNode;
	
	/**
	 * 点击的回调接口
	 */
	public OnTreeNodeClickListener onTreeNodeClickListener;
	private Context mContext;

	
	public DynaTreeAdapter(ListView mTree, Context context, String json) throws IllegalArgumentException,
			IllegalAccessException
	{
		mContext = context;
		mInflater = LayoutInflater.from(mContext);
		
		/**
		 * 对所有的Node进行排序
		 */
		mAllNodes = TreeHelper.getAllSortsNodes(json);
		
		/**
		 * 过滤出可见的Node
		 */
		mNodes = TreeHelper.filterVisibleNode(mAllNodes);
		
		/**
		 * 设置节点点击时，可以展开以及关闭；并且将ItemClick事件继续往外公布
		 */
		mTree.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id)
			{
				
				if(!mNodes.get(position).isLeaf())
				{
					mClickNode=mNodes.get(position);
					if(mClickNode.getChildren().size()!=0)
						expandOrCollapse(position);
				}
				if (onTreeNodeClickListener != null)
				{
					onTreeNodeClickListener.onClick(mNodes.get(position),
							position);
	
				}
			}
		});
	}
	
	/**
	 * 相应ListView的点击事件 展开或关闭某节点
	 * 
	 * @param position
	 */
	public void expandOrCollapse(int position)
	{
		Node n = mNodes.get(position);

		if (n != null)// 排除传入参数错误异常
		{
			if (!n.isLeaf())
			{
				n.setExpand(!n.isExpand());
				mNodes = TreeHelper.filterVisibleNode(mAllNodes);
				notifyDataSetChanged();// 刷新视图
			}
		}
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Node node = mNodes.get(position);
		convertView = getConvertView(node, position, convertView, parent);
		// 设置内边距
		convertView.setPadding(node.getLevel() * 30, 3, 3, 3);
		return convertView;
	}
	
	public View getConvertView(Node node , int position, View convertView, ViewGroup parent)
	{
		ViewHolder viewHolder = null;
		if (convertView == null)
		{
			convertView = mInflater.inflate(R.layout.list_item, parent, false);
			viewHolder = new ViewHolder();
			viewHolder.icon = (ImageView) convertView
					.findViewById(R.id.id_treenode_icon);
			viewHolder.label = (TextView) convertView
					.findViewById(R.id.id_treenode_label);
			convertView.setTag(viewHolder);

		} else
		{
			viewHolder = (ViewHolder) convertView.getTag();
		}

		if (node.getIcon() == -1)
		{
			viewHolder.icon.setVisibility(View.INVISIBLE);
		} else
		{
			viewHolder.icon.setVisibility(View.VISIBLE);
			viewHolder.icon.setImageResource(node.getIcon());
		}
		viewHolder.label.setText(node.getName());
		
		return convertView;
	}

	@Override
	public int getCount()
	{
		return mNodes.size();
	}

	@Override
	public Object getItem(int position)
	{
		return mNodes.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}
	
	private final class ViewHolder
	{
		ImageView icon;
		TextView label;
	}
	
	public void setOnTreeNodeClickListener(
			OnTreeNodeClickListener onTreeNodeClickListener2)
	{
		this.onTreeNodeClickListener = onTreeNodeClickListener2;
	}

	/**
	 * 添加子节点
	 * @param childJson
	 */
	public void addChild(String childJson) {
		// TODO Auto-generated method stub
		//添加子节点
		TreeHelper.addChildNode(mClickNode,childJson);
		//把添加的子节点添加到所有节点中
		mAllNodes.addAll(mNodes.indexOf(mClickNode)+1, mClickNode.getChildren());
		expandOrCollapse(mNodes.indexOf(mClickNode));
		
	}
}
